<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/common/include/taglibs.jsp"%>
<script type="text/javascript">

$(document).ready(function(){

});

//데이터 저장
function submitData(){
	$("input[name='hps']").each(function(){
		if(""==$(this).val()){
			$(this).parent().parent().remove();
		}else{
			$(this).val($(this).val().split("-").join(""));
		}
	});
// 	return false;
	if($("#adm_seq").val()==''){
		alert("업체를 선택하세요");
		return;
	}
	
	$.ajax({
		url: "./insertUsr.do",
		dataType: "json",
		data:$("#frm").serialize(),
		success : function (json){
			if("0"==json.returnCd){
				showMsg("저장되었습니다");
			}else if("1"==json.returnCd){
				var alertMsg = "";
				$.each(json.list, function (i) {
					if(""!=alertMsg){
						alertMsg+="\n";
					}
					alertMsg+="회원번호 : "+this.mem_seq+", 닉네임 : " + this.nick+", 전화번호 : "+this.hp;
				});
				alertMsg+="과 중복됩니다";
				showMsg(alertMsg);
			}else{
				showMsg("저장실패");
			}
		},
		error : function(error){
			seqChk = true;
			alert('접속지연이 발생했습니다. 몇분후에 다시 확인해주세요\n'+error);
		}
	});
	
}
function goList(){
	location.href="./usrList.do";
}
function getEnc(){
	if(window.hwInfo){
		$("#enc").val(window.hwInfo.getInfo());	
	}
}

function showMsg(msg){
	if(window.hwInfo){
		$("#enc").val(window.hwInfo.showMsg(msg));	
	}else{
		alert(msg);
	}
}

var addUsr =1;
function addUsrs(){
	addUsr++;
	var html= "<tr id='hp_"+addUsr+"'>"+
	"	<td>연락처</td>"+
	"	<td><input name=\"hps\" type=\"tel\" value=\"\" /><button onclick=\"removeUsr('hp_"+addUsr+"');\">삭제</button> </td>"+
	"</tr>";
	$("#hp").after(html);
}

function removeUsr(id){
	$("#"+id).remove();
}
</script>

<form id="frm" action="./insertUsr.do" method="post" >
<input id="enc" name="enc" value="" type="hidden"/>

<table>
	<tr>
		<td>업체</td>
		<td>
				<select name="adm_seq" id="adm_seq">
					<option value="">업체</option>
	<c:if test="${cdList3 !=null && fn:length(cdList3)>0 }">
		<c:forEach var="cd" items="${cdList3}" varStatus="status">
				<option value="${cd.adm_seq }" <c:if test="${param.src_search3==cd.adm_seq }"> selected="selected"</c:if>>${cd.company }</option>
		</c:forEach>
	</c:if>
				</select> 
		</td>
	</tr>
	<tr>
		<td>닉네임</td>
		<td><input name="nick" type="text" value="" /> </td>
	</tr>
	<tr id="hp">
		<td>연락처</td>
		<td><input name="hps" type="tel" value="" /><button onclick="addUsrs();return false;">추가</button> </td>
	</tr>
	<tr>
		<td>등급</td>
		<td>
			<select name="user_type">
				<option value="">선택하세요</option>
	<c:if test="${cd2List !=null && fn:length(cd2List)>0 }">
		<c:forEach var="cd" items="${cd2List}" varStatus="status">
				<option value="${cd.code_no }">${cd.code_nm }</option>
		</c:forEach>
	</c:if>

			</select>
		</td>
	</tr>
	
	<tr>
		<td>사이트</td>
		<td>
			<select name="con_site">
				<option value="">선택하세요</option>
	<c:if test="${cd1List !=null && fn:length(cd1List)>0 }">
		<c:forEach var="cd" items="${cd1List}" varStatus="status">
				<option value="${cd.code_no }">${cd.code_nm }</option>
		</c:forEach>
	</c:if>
			</select>
		</td>
	</tr>
	<tr>
		<td>비밀특이사항</td>
		<td><input name="sec_etc" type="text" value="" /> </td>
	</tr>
	<tr>
		<td>특이사항</td>
		<td><input name="etc" type="text" value="" /> </td>
	</tr>
</table>
</form>
<button onclick="submitData();">저장</button>
<button onclick="goList();">목록</button>